package main

import "fmt"

//流水线模型

//生产数据，返回通道
func produce(nums []int) chan int {
	ch := make(chan int)
	go func() {
		defer close(ch)
		for _, n := range nums {
			ch <- n
		}
	}()
	return ch
}

//对数据求平方，再返回一个通道
func square(in chan int) chan int {
	ch := make(chan int)
	go func() {
		defer close(ch)
		for num := range in {
			ch <- num * num
		}
	}()
	return ch
}

//消费数据
func consume(in chan int, f func(i int)) {
	for num := range in {
		f(num)
	}
}

func main54() {
	nums := []int{0, 1, 2, 3, 4, 5}
	ch := produce(nums)
	ch = square(ch)
	consume(ch, func(i int) {
		fmt.Println(i)
	})
}
